/**
 *
 */
package org.tio.sitexxx.service.service.stat;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.ChatRecord;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.Threads;
import org.tio.utils.thread.pool.AbstractQueueRunnable;

import com.jfinal.plugin.activerecord.Db;

/**
 * 
 * @author tanyaowu
 *
 */
public class ChatRecordRunnable extends AbstractQueueRunnable<ChatRecord> {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ChatRecordRunnable.class);

	public static ChatRecordRunnable me = new ChatRecordRunnable();

	private ChatRecordRunnable() {
		super(Threads.getTioExecutor());
	}

	/**
	 * 处理chatRecord
	 * @param chatRecord
	 * @return
	 *
	 * @author tanyaowu
	 */
	public void handler(List<ChatRecord> chatRecords) {
		Db.use(Const.Db.TIO_SITE_STAT).batchSave(chatRecords, batchCount);
	}

	static int batchCount = 50;

	/**
	 * 
	 */
	@Override
	public void runTask() {
		if (msgQueue.size() > 0) {
			ChatRecord chatRecord = null;
			List<ChatRecord> chatRecords = new ArrayList<>(msgQueue.size() + 10);
			while ((chatRecord = msgQueue.poll()) != null) {
				chatRecords.add(chatRecord);
			}

			handler(chatRecords);
		}
	}

}
